package com.jiuyun.dao.impi;

import com.jiuyun.dao.PersonalDao;
import com.jiuyun.entity.PerStudent;
import com.jiuyun.entity.PerTeacher;
import com.jiuyun.entity.PerUser;
import com.jiuyun.entity.Userrole;
import com.jiuyun.util.AliPool;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PersonalDaoimpl implements PersonalDao {
//查询信息
    @Override
    public List<PerUser> findUser(int uid) throws SQLException {
        String sql="select * from user where uid=?";
        Connection conn= AliPool.getConn();
        List<PerUser> list=new ArrayList();
        PreparedStatement pst=conn.prepareStatement(sql);
        pst.setInt(1,uid);
        ResultSet rs=pst.executeQuery();
        while (rs.next()){
            int uuid=rs.getInt(1);
            String uname=rs.getString(2);
            String upass=rs.getString(3);
            int  uuser_role_id=rs.getInt(4);
            PerUser u=new PerUser(uuid,uname,upass,uuser_role_id);
            list.add(u);
            System.out.println(u);
        }

        conn.close();
        return list;

    }

    @Override
    public List<PerTeacher> findTeacher(int teacherid) throws SQLException {
        String sql="select * from teacher where teacherid=?";
        Connection conn= AliPool.getConn();
        List<PerTeacher> list=new ArrayList();
        PreparedStatement pst=conn.prepareStatement(sql);
        pst.setInt(1,teacherid);
        ResultSet rs=pst.executeQuery();
        while (rs.next()){
            int Teacherid=rs.getInt(1);
            String tname=rs.getString(2);
            String tpass=rs.getString(3);
            String  tphone=rs.getString(4);
            String  tgob=rs.getString(5);
            PerTeacher t=new PerTeacher(Teacherid,tname,tpass,tphone,tgob);
            list.add(t);
        }
        conn.close();
        return list;
    }

    @Override
    public List<Userrole> findStu(int stid) throws SQLException {
        String sql="select * from user where uid=?";
        Connection conn= AliPool.getConn();
        List<Userrole> list=new ArrayList();
        PreparedStatement pst=conn.prepareStatement(sql);
        pst.setInt(1,stid);
        ResultSet rs=pst.executeQuery();
        while (rs.next()){
            int uid=rs.getInt(1);
            String uname=rs.getString(2);
            String upass=rs.getString(3);
            int user_role_id=rs.getInt(4);
          list.add(new Userrole(uid,uname,upass,user_role_id));
        }
        conn.close();
        return list;
    }

    //修改
    @Override
    public int updateUser(int uid, String uname, String upass) throws SQLException {
        String sql="UPDATE user SET uname=?,upass=? where uid=?";
        int num=0;
        Connection conn=AliPool.getConn();
        PreparedStatement pst=conn.prepareStatement(sql);
        pst.setString(1,uname);
        pst.setString(2,upass);
        pst.setInt(3,uid);
        num=pst.executeUpdate();
        conn.close();
        return num;
    }

    @Override
    public int updateTeacher(int tid,String tname,String tpass,String tphone) throws SQLException {
        String sql="UPDATE teacher SET tname=?,tpass=?,tphone=? where teacherid=?";
        int num=0;
        Connection conn=AliPool.getConn();
        PreparedStatement pst=conn.prepareStatement(sql);
        pst.setString(1,tname);
        pst.setString(2,tpass);
        pst.setString(3,tphone);
        pst.setInt(4,tid);
        num=pst.executeUpdate();
        conn.close();
        return num;
    }


    @Override
    public int updateStudent(int uid,String uname,String upass) throws SQLException {
        String sql="UPDATE user set uname=?,upass=? where uid=?";
        int num=0;
        Connection conn=AliPool.getConn();
        PreparedStatement pst=conn.prepareStatement(sql);
        pst.setString(1,uname);
        pst.setString(2,upass);
        pst.setInt(3,uid);
        num=pst.executeUpdate();
        conn.close();
        return num;
    }

    public static void main(String[] args) throws SQLException {
        PersonalDao dao = new PersonalDaoimpl();
      //  System.out.println(dao.updateStudent(222,"黄雨1","1234","女","111"));
       // System.out.println(dao.updateUser(1,"admm","upss"));
        System.out.println(dao.findTeacher(1));
    }


}
